package com.zsr.connection;

import org.junit.Test;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC连接测试
 */
public class ConnectionTest {
    /**
     * 连接方式一
     * 说明：上述代码中显式出现了第三方数据库的API
     *
     * @throws SQLException
     */
    @Test
    public void testConnection1() throws SQLException {
        Driver driver = new com.mysql.jdbc.Driver();
        /**
         * jdbc:mysql:协议
         * localhost  IP地址
         * 3306  mysql默认端口号
         * book 数据库名
         */
        String url = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "root");

        Connection connect = driver.connect(url, properties);
        System.out.println(connect);

    }

    /**
     * 连接方式二
     * 说明：相较于方式一，这里使用反射实例化Driver，不在代码中体现第三方数据库的API。体现了面向接口编程
     * 思想。
     */
    @Test
    public void testConnection2() {
        try {
            //1.实例化Driver
            String className = "com.mysql.jdbc.Driver";
            Class aClass = Class.forName(className);
            Driver driver = (Driver) aClass.newInstance();

            //2.提供url
            String url = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8";

            //3.提供Properties对象
            Properties properties = new Properties();
            properties.setProperty("user", "root");
            properties.setProperty("password", "root");

            Connection connect = driver.connect(url, properties);
            System.out.println(connect);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 连接方式三
     * 说明：使用DriverManager实现数据库的连接。体会获取连接必要的4个基本要素
     */
    @Test
    public void testConnection3() {
        try {
            String url = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8";
            String user = "root";
            String password = "root";
            String driverName = "com.mysql.jdbc.Driver";

            //2.实例化Driver
            Class aClass = Class.forName(driverName);
            Driver driver = (Driver) aClass.newInstance();
            //3.注册驱动
            DriverManager.registerDriver(driver);
            //4.获取链接
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testConnection4() {
        try {
            //1.数据库连接的4个基本要素：
            String url = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8";
            String user = "root";
            String password = "root";
            String driverName = "com.mysql.jdbc.Driver";
            //2.加载驱动 （①实例化Driver ②注册驱动）
            Class.forName(driverName);

            //Driver driver = (Driver) clazz.newInstance();
            //3.注册驱动
            //DriverManager.registerDriver(driver);
            /*
            可以注释掉上述代码的原因，是因为在mysql的Driver类中声明有：
            static {
                try {
                    DriverManager.registerDriver(new Driver());
                } catch (SQLException var1) {
                    throw new RuntimeException("Can't register driver!");
                }
            }
            */
            //3.获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * ①实现了代码和数据的分离，如果需要修改配置信息，直接在配置文件中修改，不需要深入代码 ②如果修改了
     * 配置信息，省去重新编译的过程
     * @throws Exception
     */
    @Test
    public void testConnection5() throws Exception {
        //1.加载配置文件
        InputStream is =
                ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);
        //2.读取配置信息
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");
        //3.加载驱动
        Class.forName(driverClass);
        //4.获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
    }



}
